//
//  ViewController.m
//  FMdb
//
//  Created by 沈成才 on 15-4-27.
//  Copyright (c) 2015年 孙俊夫. All rights reserved.
//

#import "ViewController.h"
#import "Header.h"
#import "MySqliteDo.h"
#import "Student.h"
#import "MySqlite.h"
#import "Teacher.h"


@interface ViewController ()
@property (nonatomic, strong) UILabel *name;
@property (nonatomic, strong) UILabel *age;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    NSString *str = mainUrl;
//    NSLog(@"%@",str);
//    UILabel *label = [[UILabel alloc] init];
//    label.text = str;
//    label.frame = CGRectMake(100, 100, 200, 30);
//    label.textAlignment = NSTextAlignmentCenter;
//    //label.backgroundColor = [UIColor redColor];
//    label.backgroundColor = labelColor;
//    [self.view addSubview:label];
    
   _name = [[UILabel alloc] init];
    _name.backgroundColor = [UIColor redColor];
    _name.frame = CGRectMake(100, 100, 200, 30);
    [self.view addSubview:_name];
    
   _age = [[UILabel alloc] init];
    _age.backgroundColor = [UIColor blueColor];
    _age.frame = CGRectMake(100, 200, 200, 30);
    [self.view addSubview:_age];
    
    UIButton *buttonName = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonName.frame = CGRectMake(100, 300, 60, 20);
    [buttonName setTitle:@"收藏" forState:UIControlStateNormal];
    [buttonName addTarget:self action:@selector(shoucang:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonName];
    
    UIButton *buttonAge = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonAge.frame = CGRectMake(180, 300, 60, 20);
    [buttonAge setTitle:@"删除" forState:UIControlStateNormal];
    [buttonAge addTarget:self action:@selector(shanchu:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonAge];
    
    UIButton *buttonFIND = [UIButton buttonWithType:UIButtonTypeSystem];
    buttonFIND.frame = CGRectMake(180, 350, 60, 20);
    [buttonFIND setTitle:@"查询" forState:UIControlStateNormal];
    [buttonFIND addTarget:self action:@selector(chaxun:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonFIND];
    
    
}

- (void)shoucang:(UIButton *)button
{
    NSLog(@"收藏");
//    Student *stu = [[Student alloc] init];
//    stu.name = @"小明";
//    stu.age = 15;
//    
//   
//    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS 'student'('name' TEXT, 'age' INT PRIMARY KEY)"];
//    NSString *tea = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS 'teacher'('name' TEXT PRIMARY KEY, 'hobby' TEXT)"];
//    NSString *insertStu =[NSString stringWithFormat:@"INSERT INTO 'student'('name','age') VALUES ('%@','%d')",stu.name,stu.age];
//    [MySqlite createTableWith:sql];
//    [MySqlite createTableTeacher:tea];
//    [MySqlite insertStuWithSql:insertStu];
    NSString *str1 = @"孙俊夫";
    NSString *str = @"打篮球";
    
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS 'teacher'('name' TEXT, 'hobby' TEXT PRIMARY KEY)"];
    [MySqlite createTableTeacher:sql];
    
    NSString *insertTea =[NSString stringWithFormat:@"INSERT INTO 'teacher'('name','hobby') VALUES ('%@','%@')",str1,str];
    
    [MySqlite insertTeaWithSql:insertTea];
    
    
}

-(void)shanchu:(UIButton *)button
{
    NSLog(@"删除");
    
    NSString *delete = [NSString stringWithFormat:@"delete from teacher"];
    [MySqlite deleteTableTea:delete];
    NSLog(@"%@",_name.text);
    
    
}

-(void)chaxun:(UIButton *)button
{
//    NSString *select = [NSString stringWithFormat:@"select * from student"];
//   NSMutableArray *arr =  [MySqlite selectStuWitSql:select];
//    Student *stu = [[Student alloc] init];
//    stu = [arr objectAtIndex:0];
//    _name.text = stu.name;
//    NSString *age = [NSString stringWithFormat:@"%d",stu.age];
//    _age.text = age;
    
    NSString *select = [NSString stringWithFormat:@"select *from teacher"];
    NSMutableArray *arr = [MySqlite selectTeaWitSql:select];
    Teacher *tea = [[Teacher alloc] init];
    tea = [arr objectAtIndex:0];
    _name.text = tea.name;
    _age.text = tea.hobby;
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
